<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="产品分类" prop="goodsId">
        <treeselect v-model="queryParams.goodsType" :options="typeOptions" style="width: 240px;"
                    placeholder="请选择产品分类" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编号" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="goodsModel">
        <el-input v-model="queryParams.goodsModel" placeholder="请输入产品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品数量" prop="count">
        <el-input v-model="queryParams.count" placeholder="请输入产品数量"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="使用理由" prop="reason">
        <el-input v-model="queryParams.reason" placeholder="请输入使用理由"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-if="queryParams.status==='0'"
                   v-hasPermi="['develop:GoodsSampleApply:add']">新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品类别" align="center" prop="params.goodsTypeName"/>
      <el-table-column label="产品编号" align="center" prop="goodsCode"/>
      <el-table-column label="产品名称" align="center" prop="goodsName"  show-overflow-tooltip/>
      <el-table-column label="产品型号" align="center" prop="goodsModel"/>
      <el-table-column label="产品数量" align="center" prop="count"/>
      <el-table-column label="使用理由" align="center" prop="reason"/>
      <el-table-column label="审核状态" align="center" prop="status"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.status === '0'"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsSampleApply:edit']">修改
          </el-button>

          <el-popconfirm :title="`确认通过 `+scope.row.goodsName+` 数据项？`"
                         v-if="scope.row.status === '0'" v-hasPermi="['develop:GoodsSampleApply:assess']"
                         @confirm="handleAssess(scope.row,'1')" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-check">出库通过
            </el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认不通过 `+scope.row.goodsName+` 数据项？`"
                         v-if="scope.row.status === '0'" v-hasPermi="['develop:GoodsSampleApply:assess']"
                         @confirm="handleAssess(scope.row,'2')" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">出库不通过
            </el-button>
          </el-popconfirm>


          <el-popconfirm :title="`确认通过 `+scope.row.goodsName+` 数据项？`"
                         v-if="scope.row.status === '1'"
                         @confirm="handleAssess(scope.row,'3')" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-check">申请入库
            </el-button>
          </el-popconfirm>


          <el-popconfirm :title="`确认通过 `+scope.row.goodsName+` 数据项？`"
                         v-if="scope.row.status === '3'" v-hasPermi="['develop:GoodsSampleApply:assess']"
                         @confirm="handleAssess(scope.row,'4')" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-check">入库通过
            </el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认不通过 `+scope.row.goodsName+` 数据项？`"
                         v-if="scope.row.status === '3'" v-hasPermi="['develop:GoodsSampleApply:assess']"
                         @confirm="handleAssess(scope.row,'5')" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">入库不通过
            </el-button>
          </el-popconfirm>





          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                         v-if="scope.row.status === '0'"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:GoodsSampleApply:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <goodsSampleApply-form ref="form" @ok="getList"></goodsSampleApply-form>
  </div>
</template>

<script>
import {deleteAction, getAction, updateAction} from '@/api/manage'
import GoodsSampleApplyForm from '@/views/develop/goodsSampleApply/module/GoodsSampleApplyForm'
import Treeselect from "@riophae/vue-treeselect";
import {listByLazyTree} from "@/api/sysArea/area";

export default {
  name: "GoodsSampleApplyList",
  components: {
    Treeselect,
    GoodsSampleApplyForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检样品申请表格数据
      dataList: [],
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        count: null,
        reason: null,
        status: null,
      },
      url: {
        list: '/goodsSampleApply/goodsSampleApply/list',
        assess: '/goodsSampleApply/goodsSampleApply/assess',
        remove: '/goodsSampleApply/goodsSampleApply/',
        export: '/goodsSampleApply/goodsSampleApply/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    let that = this;
    that.queryParams.status = that.$route.query.status;
    this.getList();
    this.getTreeselect();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {

    /** 查询类别 */
    getTreeselect() {
      listByLazyTree().then(response => {
        this.typeOptions = response.data;
      });
    },

    /** 查询质检样品申请列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `goodsSampleApply_${new Date().getTime()}.xlsx`)
    },


    handleAssess(row,status){
      let form  = {};
      form.id = row.id;
      form.status = status;
      form.goodsId = row.goodsId;
      form.count = row.count;
      updateAction(this.url.assess, form).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    }





  }
};
</script>
